// hyb_yl/userLife/pages/deveVoice/deveVoice.js

const bgM = wx.createInnerAudioContext();
const app = getApp()
const ba = wx.getBackgroundAudioManager();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    isplay:true,
    itemIndex:'',
    voiceList:{},
    list:[],
    min: 0, //slider最小值
    max: 0, //slider最大值:音频时长
    value: 0, //slider当前进度
    now: '00:00', //播放条当前时间
    long: '00:00', //音频总时长
    maskShow:false,
  },
  play: function () {
    this.setData({
      isplay: !this.data.isplay
    })
    if (this.data.isplay) {
      this.start()
    } else {
      this.pause()
    }
  },
  /**
   * 播放
   */
  start() {
    ba.play();
    this.setData({
      isplay:true
    })
  },
  /**
   * 暂停
   */
  pause() {
    ba.pause();
    this.setData({
      isplay:false
    })
  },
  /**
   * 播放新的音频
   */
  setAudio() {
    var that = this;
    var data = that.data.list[app.globalData.playIndex]
    console.log(data)
    that.setData({
      max: 100,
      value: 0,
      coverImgUrl: data.h_pic,
      title: data.title,
      singer: data.singer,
      epname: data.epname,
      voiceList:data
    })

    app.globalData.currentTime = 0;
    app.globalData.duration = 0;
    ba.title = data.h_title;
    ba.epname = data.h_title;
    ba.singer = data.singer;
    ba.coverImgUrl = data.h_pic;
    ba.src = data.audios;
  },
  /**
   * 设置音频封面信息，除src
   */
  setAudioCover() {
    var that = this;
    var data = app.globalData.list[app.globalData.playIndex]
    that.setData({
      coverImgUrl: data.coverImgUrl,
      title: data.h_title,
      singer: data.singer,
      epname: data.epname,
      voiceList: data
    })

    ba.title = data.h_title;
    ba.epname = data.epname;
    ba.singer = data.singer;
    ba.coverImgUrl = data.coverImgUrl;
  },
  /**
   * 播放下一首
   */
  playNext() {
     var that = this;
    let playIndex = 0;
    if (app.globalData.playIndex < (app.globalData.list.length - 1)) {
      playIndex = app.globalData.playIndex + 1;
    }
    app.globalData.playIndex = playIndex;
    that.setAudio();
    that.setData({
      isplay: true,
      voiceList: app.globalData.list[app.globalData.playIndex],
      itemIndex :app.globalData.playIndex
    })
  },
  /**
   * 播放上一首
   */
  playPrev() {
    var that = this;
    var playIndex = app.globalData.list.length - 1;
    if (app.globalData.playIndex > 0) {
      playIndex = app.globalData.playIndex - 1;
    }
    app.globalData.playIndex = playIndex;
    that.setAudio();
    that.setData({
      isplay: true,
      voiceList: app.globalData.list[app.globalData.playIndex],
      itemIndex: app.globalData.playIndex
    })
    // if(this.data.isplay){

    // }
  },
  /**
   * 滑动
   */
  seek(e) {
    var that = this;
    var value = e.detail.value;
    ba.seek(value);
  },
  /**
   * 滑动中
   */
  seeking(e) {
    var that = this;
    var value = e.detail.value;
    var now = that.formatS2M(value);
    // 显示当前滑动的时间
    that.setData({
      now: now
    })
  },
  /**
   * 将秒格式化为如 02:05
   */
  formatS2M(seconds) {
    var durationMinute = parseInt(seconds / 60);
    var durationSecond = parseInt(seconds % 60);
    if (durationSecond < 10) {
      durationSecond = '0' + durationSecond;
    }
    if (durationMinute < 10) {
      durationMinute = '0' + durationMinute;
    }
    return (durationMinute + ':' + durationSecond)
  },
  list:function(){
    this.setData({
      maskShow:!this.data.maskShow
    })
  },
  closeList:function(){
    this.setData({
      maskShow:!this.data.maskShow
    })
  },
  itemBtn:function(e){
    console.log(e)
    var that = this
    var playIndex = e.currentTarget.dataset.itemindex
    app.globalData.playIndex = playIndex;
    var list = that.data.list
    var voiceList = list[playIndex]
    that.setAudio();
    that.setData({
      isplay: true,
      itemIndex:playIndex,
      voiceList:voiceList
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    console.log(app.globalData.playIndex)
    console.log(options)
    var playIndex= options.index
   var itemIndex = options.itemIndex;
    var list = JSON.parse(options.list)
    console.log(list)
    this.setData({
      itemIndex: app.globalData.playIndex,
      // voiceList: list[itemIndex],
      list:list
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    // 音频播放进度实时回调
    ba.onTimeUpdate(function () {
      var currentTime = parseInt(ba.currentTime);
      if (currentTime > 0 && currentTime != app.globalData.currentTime) {
        app.globalData.currentTime = currentTime;
        let now = that.formatS2M(currentTime);
        app.globalData.currentTime = currentTime;
        that.setData({
          value: currentTime,
          now: now
        })
        // console.log('当前时间', ba.currentTime, '总时长', ba.duration);
      }
      if (ba.duration != that.data.max) {
        let long = that.formatS2M(ba.duration);
        app.globalData.duration = ba.duration;
        that.setData({
          max: ba.duration,
          long: long
        })
        console.log('初始化音频时间，包括时长和显示格式的时长（如：00:21）')
      }
    });
    ba.onCanplay(function () {
      console.log('onCanplay', ba, '总时长', ba.duration);
    });
    ba.onEnded(function () {
      console.log('onEnd')
      that.setAudio();
    });
    ba.onNext(function () {
      console.log('onNext')
      that.playNext();
    });
    ba.onPrev(function () {
      console.log('onPrev')
      that.playPrev();
    });
    ba.onEnded(function () {
      console.log('onEnded')
      that.playNext();
    });
    ba.onStop(function () {
      console.log('onStope')
    
    });
    ba.onWaiting(function () {
      console.log('onWaiting', '正在拼命加载中...')
     
    });
    // 处理从其他页面进入之前，该播放器已经实例过
    if (ba.duration > 0) {
      that.setData({
        list: app.globalData.list
      }, function () {
        that.setAudioCover();
      })
      return;
    }
    // 初始加载完成后，将音频列表放到globalData中，缓存播放列表
    app.globalData.list = that.data.list;
    that.setAudio();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      isplay: false
    })
    this.pause()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})